<?
require_once("inc.init.php");

// Getting content
$indexField = 'userID';
$myUser = new AdMinUser();
$myUser->setDBLink($dbLink);
$_GET[$indexField]? $myUser->get((int)$_GET[$indexField]) : null;

// looking for errors
$messenger = new Messenger();

// MODIFY USER DATA.
if ($_POST['action'] == 'submit')
{
    //Setting
    $myUser->set(array('username'        => txt2sql($_POST['username'], false, $dbLink),
                       'password'        => txt2sql($_POST['password'], false, $dbLink),
                       'email'           => txt2sql($_POST['email'], false, $dbLink),
                       'name'            => txt2sql($_POST['name'], false, $dbLink),
                       'last_name'       => txt2sql($_POST['last_name'], false, $dbLink),
                       'active'          => $_POST['active']=="Y"? 'Y' : 'N',
                       'superuser'       => 'Y',
                       'userID_modified' => $user->getID()));

    // username checking
    if(eregi("^[\s]*$", $_POST['username']) || !eregi("^(.){4,12}$", $_POST['username']) || !eregi("^[a-zA-Z0-9_-]*$", $_POST['username']))
        $messenger->addMessage("Please enter a valid user name (from 6 to 12 numbers and letters \"-\" y \"_\").");
    if ($_POST['username'] != $_POST['old_username'])
    {
        $result = mysql_query("SELECT username FROM users WHERE username LIKE '". mysql_escape_string($_POST['username']) ."'", $dbLink);
        if (mysql_num_rows($result)>0)
        {
            $messenger->addMessage("The user selected already exists in our database. Please enter a different one.");
        }
    }

    // email checking
    if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", trim($_POST['email'])))
    {
        if ($_POST['email'] != $_POST['old_email'])
        {
            $result = mysql_query("SELECT email FROM users WHERE email ='". mysql_escape_string($_POST['email']) ."'", $dbLink);
            if (mysql_num_rows($result)>0)
            {
                $messenger->addMessage("EMAIL chosen already exists in our database. Please enter a different one.");
            }
        }
    }
    else $messenger->addMessage("Please enter a valid EMAIL.");

    // password checking
    if(isset($_POST['password']) && eregi("^[\s]*$", $_POST['password']) || !eregi("^(.){4,12}$", $_POST['password']))
        $messenger->addMessage("Please enter a valid Password (4 to 12 characters).");
    if($_POST['password']!=$_POST['password_check'])
        $messenger->addMessage("Please enter your PASSWORD equal to valid PASSWORD CONFIRMATION.");

    // SAVING record
    if ($messenger->countMessages()==0) // if no errors
    {
        if ($user->isEnabledTo("write", MODULE_ID))
        {
            if (($_GET[$indexField]? $myUser->update() : $myUser->create()))
            {
                header("location: ./index.php");
                exit();
            }
            else $messenger->addMessage("Failed to save the record: ". mysql_error());
        } else $messenger->addMessage("You do not have permission for this action");
    }
    $msjError = $messenger->getMessages();
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>AdMin | <?= $GLOBALS["website"]["name"] ?></title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 
    <link rel="stylesheet" href="<?= $GLOBALS['CONF']['url']['admin']?>/css/bootstrap.css">
    <link rel="stylesheet" href="<?= $GLOBALS['CONF']['url']['admin']?>/css/font-awesome.min.css">
    <link rel="stylesheet" href="<?= $GLOBALS['CONF']['url']['admin']?>/css/plugin.css">
    <link rel="stylesheet" href="<?= $GLOBALS['CONF']['url']['admin']?>/css/font.css" cache="false">
    <link rel="stylesheet" href="<?= $GLOBALS['CONF']['url']['admin']?>/css/style.css">
  <!--[if lt IE 9]>
    <script src="js/ie/respond.min.js" cache="false"></script>
    <script src="js/ie/html5.js" cache="false"></script>
  <![endif]-->
</head>
<script language="JavaScript" type="text/javascript" src="<?= $GLOBALS["CONF"]["url"]["js"] ?>/lib/tiny_mce/tiny_mce_gzip.js"></script>
<script type="text/javascript">
    tinyMCE_GZ.init({ });
</script>
<!-- Needs to be seperate script tags! -->
<script language="JavaScript" type="text/javascript">
    tinyMCE.init({
        theme: 'advanced',
        theme_advanced_styles : "Texto 1=texto;Texto 2=epb3;Texto 3=titulares",
        content_css : "<?= $GLOBALS["CONF"]["url"]["js"] ?>/css/style.css",
        mode : "textareas"
    });
</script>
</head>
<body>
    <section id="content">
        <section class="main padder">
            <div class="row">
                <br>
                <!-- .breadcrumb -->
                <ul class="breadcrumb">
                    <li><a href="."><?= $module->name ?></a></li>
                    <li class="active"><?
                        if ($myUser->getID())
                        {
                            ?><a href="#"><?= $obj->name ?></a><? 
                        }
                        else
                        {
                            echo ucfirst($lang->translate("adding")) ." ". strtolower($module->unit);
                        }
                        ?>
                    </li>
                </ul>
                <!-- / .breadcrumb -->
                <div class="col-sm-12">
                    <section class="panel">
                        <div class="panel-body">
                            <? 
                                if($messenger->countMessages())
                                {
                                    ?> 
                                    <div class="alert <?= $messenger->_messagesType==1 ? 'alert-success': 'alert-danger'?>">
                                    <button class="close" data-dismiss="alert" type="button"><i class="icon-remove"></i></button>
                                        <?=$messenger->getMessages() ?>
                                    </div>
                                    <?
                                }
                            ?>
                            <form action="" method="post" enctype="multipart/form-data" class="form-horizontal">
                                <div class="form-group">
                                    <label class="col-lg-3 control-label">System Data</label>
                                </div>
                                <div class="form-group">
                                    <label class="col-lg-3 control-label">Username</label>
                                    <div class="col-lg-3">
                                        <input class="form-control" name="username" type="text" id="username" value="<?= $myUser->username ?>" maxlength="12" tabindex="1"><input type="hidden" name="old_username" value="<?= $_POST['old_username']? $_POST['old_username'] : $myUser->username ?>">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-lg-3 control-label">Password</label>
                                    <div class="col-lg-3">
                                        <input class="form-control" name="password" type="password" id="password" value="<?= $myUser->password ?>" tabindex="2">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-lg-3 control-label">Confirm Password</label>
                                    <div class="col-lg-3">
                                        <input class="form-control" name="password_check" type="password" id="password_check" value="<?= $_POST['password_check']? $_POST['password_check'] : $myUser->password ?>" tabindex="3">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-lg-3 control-label">Personal Information</label>
                                </div>
                                <div class="form-group">
                                    <label class="col-lg-3 control-label">Name</label>
                                    <div class="col-lg-3">
                                        <input type="text" name="name" placeholder="Name" class="form-control" value="<?= $myUser->name?>">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-lg-3 control-label">Last Name</label>
                                    <div class="col-lg-3">
                                        <input type="text" name="last_name" placeholder="Last Name" class="form-control" value="<?= $myUser->last_name?>">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-lg-3 control-label">Email</label>
                                    <div class="col-lg-3">
                                        <input type="text" name="email" placeholder="test@test.com" class="form-control" value="<?= $myUser->email?>">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-lg-3 control-label">Active</label>
                                    <div class="col-lg-8">
                                        <input type="checkbox" <?= $myUser->active == 'Y' ? "checked" : '' ?> name="active" value="Y">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-lg-9 col-lg-offset-3">
                                        <button type="submit" class="btn btn-primary">Save</button> o 
                                        <a href="."><span> [Cancel]</span></a>
                                    </div>
                                </div>
                                <input type="hidden" name="action" value="submit" id="action">
                            </form>
                            <!--<div class="well m-t">
                                <div class="row">
                                    <div class="col-xs-12">
                                        <strong>Info de sistema </strong><br>
                                        <?= $module->unit ?> id: <?= $myUser->getID() ?><br>
                                    </div>
                                    <div class="col-xs-6">
                                        Fecha de creaci&oacute;n : <?= $myUser->date_created ?><br>
                                        Fecha &uacute;ltimo cambio: <?= $myUser->date_modified ?></br>
                                    </div>
                                    <div class="col-xs-6">
                                        Usuario que cre&oacute;: <?= $myUser->userCreated ?></br>
                                        Usuario que modific&oacute;: <?= $myUser->userModified ?></br>
                                    </div>
                                </div>
                            </div>-->
                        </div>
                    </section>
                </div>
            </div>
        </section>
    </section>
</body>
</html>
